includeTool << gant.tools.Execute
includeTargets << grailsScript("Init")
Ant.property(environment: 'env')

//def pluginBasedir = "${solr-hierarchicalPluginDir}"
def pluginBasedir = "${pluginsHome}/simple-solr-0.1/"
def solrHome = "${grails.util.BuildSettingsHolder.getSettings().projectWorkDir}/solr-home"
def solrStopPort = "8079"
def solrPort = "8983"
def solrHost = "localhost"

target ( startsolr: "Start Solr Jetty Instance") {
    depends("stopsolr")
    depends("init")
		
		// overlay the schema.xml config file in the apps grails-app/conf/solr directory (and other conf files)
		Ant.copy(todir:"${solrHome}/solr/conf", failonerror: false) {
			fileset( dir: "${basedir}/grails-app/conf/simplesolr")
		}
		
		Ant.unzip(src:"${solrHome}/webapps/solr.war", dest:"${solrHome}/tmp")
		
		Ant.copy(todir:"${solrHome}/tmp/WEB-INF/classes/org/simplesolr/similarity", failonerror: false) {
			fileset( dir: "${basedir}/target/classes/org/simplesolr/similarity")
		}

		Ant.zip(destfile:"${solrHome}/webapps/solr.war", basedir:"${solrHome}/tmp", update: true)
		
		Ant.delete(dir:"${solrHome}/tmp")
		// pause just for a bit more time to be sure Solr Stopped
		Thread.sleep(1000)

		// start it up
		Ant.java ( jar:"${solrHome}/start.jar", dir: "${solrHome}", fork:true, spawn:true, classpath:"${solrHome}") {
			jvmarg(value:"-DSTOP.PORT=${solrStopPort}")
			jvmarg(value:"-DSTOP.KEY=secret")
			arg(line:"etc/jetty.xml")
		}
		
			
		println "Starting Solr - Solr HOME is ${solrHome}"
		println "-----------"
		println "Solr logs can be found here: ${solrHome}/logs"
		println "Console access: http://localhost:${solrPort}/solr/"
		println "-----------"
}

setDefaultTarget ( "startsolr" )

target(checkport: "Test port for solr") {
  condition(property: "solr.not.running") {
    not {
      socket(server: solrHost, port: solrStopPort)      
    }
  }
}

target(stopsolr: "Stop Solr") {
  depends("checkport", "init")

	if ( !Boolean.valueOf(Ant.project.properties.'solr.not.running') ) {
    println "Stopping Solr..."
  	java ( jar:"${solrHome}/start.jar", dir: "${solrHome}", fork:true) {
      jvmarg(value:"-DSTOP.PORT=${solrStopPort}")
  		jvmarg(value:"-DSTOP.KEY=secret")
  		arg(value: "--stop")
  	}
	}
 
}

target(init: "Create the solr-home directory") {
  // copy over the resources for solr home
	Ant.mkdir(dir: "${solrHome}")
	Ant.copy(todir:"${solrHome}") {
		fileset( dir: "${pluginBasedir}/src/solr-local", )
	}
}
